package com.lanou.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lanou.bean.Designer;
import com.lanou.bean.Product;
import com.lanou.bean.ProductPhoto;
import com.lanou.bean.User;
import com.lanou.bean.UserComments;
import com.lanou.mapper.CompanyMapper;
import com.lanou.mapper.ProductMapper;
import com.lanou.mapper.ProductPhotoMapper;
import com.lanou.mapper.UserCommentMapper;
import com.lanou.service.ProductService;
import com.lanou.utils.AjaxResult;
import com.lanou.utils.TimeUtils;
import org.apache.ibatis.annotations.Param;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * Created by dllo on 18/3/12.
 */
@Service
public class ProductServiceImp implements ProductService {
    @Resource
    private ProductMapper productMapper;
    @Resource
    private UserCommentMapper userCommentMapper;
    @Resource
    private ProductPhotoMapper productPhotoMapper;


    /**
     * @param id 产品Id
     * @return 通过产品Id查询某个产品的详细信息
     * 开发人:孙涛
     */

    public AjaxResult selectOneProduct(Integer id) {

        AjaxResult ajaxResult = new AjaxResult();
        Product product = productMapper.selectOneProduct(id);
        ajaxResult.setData(product);
        return ajaxResult;
    }

    /**
     * @param pageNum  当前页数
     * @param pageSize 每页显示数据数量
     * @param id       公司id
     * @return 通过公司id查询公司旗下的产品, 分页显示
     * 开发人:孙涛
     */
    public PageInfo<Product> pageSelectAll(Integer pageNum, Integer pageSize, Integer id) {
        pageNum = pageNum == null ? 1 : pageNum;//默认为第一页
        pageSize = pageSize == null ? 2 : pageSize;//默认显示条数据
        PageHelper.startPage(pageNum, pageSize);
        List<Product> products = productMapper.selectAllProduct(id);
        PageInfo<Product> productPageInfo = new PageInfo<Product>(products);
        return productPageInfo;
    }

    /**
     * @param pageNum  当前页数
     * @param pageSize 每页显示数据数量
     * @param id       公司id
     * @return 通过ajaxResult返回数据接口
     * 开发人:孙涛
     */
    public AjaxResult pageSelectAllResult(Integer pageNum, Integer pageSize, Integer id) {
        AjaxResult ajaxResult = new AjaxResult();
        PageInfo<Product> productPageInfo = pageSelectAll(pageNum, pageSize, id);
        ajaxResult.setData(productPageInfo);
        return ajaxResult;
    }

    /**
     * @param pageNum    当前页数
     * @param pageSize   每页显示数据数量
     * @param proStareId 工地施工进度Id
     * @return 通过工地进度Id查询产品(正在施工, 竣工)
     * 开发人:孙涛
     */
    public PageInfo<Product> selectProductByState(Integer pageNum, Integer pageSize, Integer proStareId, Integer companyId) {
        pageNum = pageNum == null ? 1 : pageNum;//默认为第一页
        pageSize = pageSize == null ? 2 : pageSize;//默认显示条数据
        PageHelper.startPage(pageNum, pageSize);
        List<Product> productList = productMapper.selectProductByState(proStareId, companyId);
        PageInfo<Product> productPageInfo = new PageInfo<Product>(productList);
        return productPageInfo;
    }

    /**
     * @param proStateId 工地施工进度Id
     * @param pageNum    当前页数
     * @param pageSize   每页显示数据数量
     * @return 通过ajaxResult返回数据接口
     * 开发人:孙涛
     */
    @Override
    public AjaxResult pageSelectByStateResult(Integer proStateId, Integer pageNum, Integer pageSize, Integer companyId) {
        AjaxResult ajaxResult = new AjaxResult();
        PageInfo<Product> productList = selectProductByState(pageNum, pageSize, proStateId, companyId);
        ajaxResult.setData(productList);
        return ajaxResult;
    }

    /**
     * 徐鸣-客户添加需求
     *
     * @param pUserName
     * @param pPhone
     * @param pCity
     * @param pDecorateTime
     * @param pArea
     * @param pProductType
     * @param pAddress
     */
    @Override
    public void insertUserDemand(String pUserName, String pPhone, String pCity,
                                 String pDecorateTime, String pArea, String pProductType,
                                 String pAddress) {
        Subject subject = SecurityUtils.getSubject();
        User user = (User) subject.getPrincipal();
        Integer pUserid = user.getU_id();
        productMapper.insertUserDemand(pUserName, pPhone, pCity, pDecorateTime, pArea, pProductType, pAddress, pUserid);
    }

    /**
     * 产品信息中获取此产品的评论信息
     *
     * @param company_id 公司id
     * @return 此产品的所有评论内容
     * 开发人:赵帅
     */
    @Override
    public AjaxResult getUserComments(Integer company_id) {
        List<UserComments> userComments = userCommentMapper.getProductComments(company_id);
        AjaxResult ajaxResult = new AjaxResult();
        ajaxResult.setCode("200");
        ajaxResult.setMsg("产品评论信息");
        ajaxResult.setData(userComments);
        return ajaxResult;
    }

    /**
     * @param productId 产品Id
     * @return 通过产品Id返回施工图片
     * 开发人:孙涛
     */
    @Override
    public AjaxResult selectConstructPhoto(@Param("productId") Integer productId) {
        AjaxResult ajaxResult = new AjaxResult();
        List<ProductPhoto> productPhotoList = selectConstruct(productId);
        ajaxResult.setData(productPhotoList);
        return ajaxResult;
    }

    public List<ProductPhoto> selectConstruct(@Param("productId") Integer productId) {
        return productPhotoMapper.selectConstructPhoto(productId);
    }

}
